package com.lvmama.rhino.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.rhino.pojo.ContinuityCode;
import com.lvmama.rhino.service.FlowStatisticsService;

@Controller
@RequestMapping("common")
public class CommonController
{
	private static Map<String,ContinuityCode> ccMap = new HashMap<String,ContinuityCode>();
	
	@Autowired
	FlowStatisticsService flowStatisticsService;
	
	@RequestMapping("appversion")
	@ResponseBody
	public Map<String, Object> querylist(@RequestParam("platForm") String platForm) 
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<ContinuityCode> list = getCodeVoLikeName(platForm);
		resultMap.put("list", list);
		return resultMap;
	}
	
	public  List<ContinuityCode>  getCodeVoLikeName(String name)
	{
		List<ContinuityCode> list = new ArrayList<ContinuityCode>();
		if(ccMap.isEmpty())
		{
			List<ContinuityCode> lists = flowStatisticsService.queryContinuityCode();
			
			if(lists != null && !lists.isEmpty())
			{
				for (ContinuityCode continuityCode : lists)
				{
					if(ccMap.get(continuityCode.getCode()) == null)
					{
						if(continuityCode.getCode() != null && continuityCode.getCode() != "")
						{
							synchronized(ccMap)
							{
								ccMap.put(continuityCode.getCode(), continuityCode);
							}
						}
						
						
					}
					
				}
			}
		}
		
		Set<Entry<String,ContinuityCode>> set = ccMap.entrySet();
		for (Entry<String, ContinuityCode> entry : set)
		{
			if(entry.getValue() != null && entry.getValue().getName()!=null
					&&entry.getValue().getName().contains(name))
			{
				list.add(entry.getValue());
			}
		}
		return list;
	}

}
